/**
 * 
 */
package jmath.io;

import javax.swing.JTextPane;
import org.springframework.util.*;
import java.util.*;

/**
 * @author timon
 * 
 */
public class JMathConsole {
	
	private final static String CONSOLE_NOT_INITIALIZED ="Необходимо инициализировать консоль";
	
	private JTextPane consolePane;
	
	private Map<String,String> consoleMessages;
	
	private String getMessage(String name){
		if(consoleMessages!=null){
			String returnString =consoleMessages.get(name);
			if(returnString!=null){
				return returnString;
			}
		}
		return "";
	}
	

	public void setConsoleMessages(Map<String, String> consoleMessages) {
		this.consoleMessages = consoleMessages;
	}

	public void setConsolePane(JTextPane consolePane) {
		this.consolePane = consolePane;
	}
	
	private JTextPane getJTextPane(){
		Assert.notNull(consolePane,CONSOLE_NOT_INITIALIZED);
		return consolePane;
	}
	
	public void clear() {		
		getJTextPane().setText("");
	}

	public void write(String message) {		
		getJTextPane().setText(getJTextPane().getText() + message);
	}

	public void writeln(String message) {
		write(message + "\n");
	}

	public void error(String errorMessage) {
		writeln(getMessage("errorPrefix") + errorMessage);
	}

	public void warn(String warnMessage) {
		writeln(getMessage("warnPrefix") + warnMessage);
	}

	public void message(String message) {
		writeln(getMessage("messagePrefix") + message);
	}
	
}
